# vim: expandtab:ts=4:sw=4
import numpy as np


class Detection(object):
    """
    This class represents a bounding box detection in a single image.
    此类表示单个图像中的边界框检测。
    Parameters
    ----------
    tlwh : array_like
        Bounding box的格式为`(top left x, top left y, width, height)`.
    confidence : float
        Detector confidence score.
    feature : array_like
       描述此图像中包含的对象的特征向量。
    """

    def __init__(self, tlwh, confidence, feature):
        self.tlwh = np.asarray(tlwh, dtype=np.float)
        self.confidence = float(confidence)
        self.feature = np.asarray(feature, dtype=np.float32)

    def to_tlbr(self):
        """
        将boundinng box由(top left x,top left y,box w,box height)格式转换为
        (左顶点x,左顶点y,右底点x,右底点y)格式
        """
        ret = self.tlwh.copy()
        ret[2:] += ret[:2]
        return ret

    def to_xyah(self):
        """
        将bounding box由(左顶点x,左顶点y,box w,box h)格式转换为
        (中心点x，中心点y，box宽高比，box h)
        """
        ret = self.tlwh.copy()
        ret[:2] += ret[2:] / 2
        ret[2] /= ret[3]
        return ret
